<template>
  <div class="container" ref="mainCon">
    <topTitle
      :titleText="titleText"
      :btnText="btnText"
      :isShowBtn="isShowBtn"
      :pathUrl="pathUrl"
      :oldUrl="oldUrl"
    ></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="监管区域" class="row-padding-bottom">
              <el-cascader
                v-model="areaId"
                :options="areasOptions"
                @change="getAreaId"
                clearable
                filterable
                placeholder="请选择"
                :props="{ checkStrictly: true }"
                :append-to-body="false"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="行业" class="row-padding-bottom">
              <el-select
                class="selectWidth"
                v-model="filterField.industryId"
                filterable
                placeholder="请选择"
                clearable
                :popper-append-to-body="false"
              >
                <el-option
                  v-for="item in industryOptions"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="事件类型" class="row-padding-bottom">
                <el-select
                  class="selectWidth"
                  v-model="filterField.typeId"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in type"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item
                :label="isCzUser() ? '发生事件' : '事件原因'"
                class="row-padding-bottom"
              >
                <el-select
                  class="selectWidth"
                  v-model="filterField.accidentId"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-tooltip
                    placement="bottom"
                    v-for="item in accident"
                    :key="item.code"
                    effect="dark"
                  >
                    <div slot="content">
                      <div
                        :style="{ maxWidth: '200px', wordBreak: 'break-all' }"
                      >
                        {{ item.name }}
                      </div>
                    </div>
                    <el-option
                      :label="item.name.substring(0, 12) + '...'"
                      :value="item.code"
                    ></el-option>
                  </el-tooltip>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="事件程度" class="row-padding-bottom">
                <el-select
                  class="selectWidth"
                  v-model="filterField.accidentRank"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in rankOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="企业名称" class="row-padding-bottom">
                <el-input
                  v-model="filterField.keyWord"
                  clearable
                  placeholder="请输入企业名称或统一社会信用代码"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item
                label="投诉时间"
                class="row-padding-bottom"
                v-show="czUser"
              >
                <timeSelection
                  @getStartTimeValue="getStartTimeValue"
                  @getEndTimeValue="getEndTimeValue"
                ></timeSelection>
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
            </el-form-item>
            <el-form-item>
              <exportBtn
                :dataLen="tableList.length"
                :filterField="filterField"
                api="api-s/report/export_JC_CS_RIGHTS_ACCIDENT"
                style="margin-top: 0px"
              ></exportBtn>
            </el-form-item>
          </el-col>
          <!-- <el-form-item>
            <el-button type="primary" icon="icon iconfont iconchazhao1" @click="searchList" class="commonBtn"></el-button>
          </el-form-item> -->
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          @sort-change="sortChange"
          :max-height="tableHeight"
          :data="tableList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            prop="typeName"
            label="事件类型"
            :formatter="formatTd"
            width="100"
          ></el-table-column>
          <el-table-column
            prop="accidentName"
            :label="isCzUser() ? '发生事件' : '事件原因'"
            width="230"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.accidentName"
                :columnName="'accidentName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="accidentDate"
            sortable="custom"
            :label="isCzUser() ? '投诉时间' : '事件时间'"
            align="center"
            width="120"
          ></el-table-column>
          <el-table-column
            prop="address"
            label="事件发生地区"
            align="center"
            :formatter="formatTd"
            min-width="160"
          ></el-table-column>
          <!-- <el-table-column prop="projectName" label="项目名称" align="center"  :formatter=formatTd></el-table-column> -->
          <el-table-column prop="projectName" label="项目名称" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="companyName" label="责任主体企业名称" align="center"  :formatter=formatTd></el-table-column> -->
          <el-table-column
            prop="companyName"
            label="责任主体企业名称"
            width="230"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.companyName"
                :columnName="'companyName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="uscc"
            label="统一社会信用代码"
            align="center"
            :formatter="formatTd"
            min-width="200"
          ></el-table-column>
          <el-table-column
            prop="accidentRankDesc"
            label="事件程度"
            align="center"
            width="100"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            label="操作"
            align="left"
            width="190"
            :formatter="formatTd"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                v-hasBtn="'importEvent:detail'"
                @click="toDetail(scope.$index, scope.row)"
                >查看</el-link
              >
              <el-link
                type="primary"
                @click="toEdit(scope.$index, scope.row)"
                v-hasBtn="'importEvent:update'"
                >编辑</el-link
              >
              <el-link
                type="primary"
                @click="toDel(scope.$index, scope.row)"
                v-if="isCzUser()"
                >删除</el-link
              >
              <!-- <span v-show="!isShowDetail && !isShowUpdate">--</span> -->
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关人员数据"></emptyTable>
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <!-- getTableList为获取子组件的数据 -->
        <Pagination
          ref="pagination"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
import scrollTable from '@/mixin/scrollTable.js'

export default {
  name: 'importEvent',
  mixins: [scrollTable],
  data() {
    return {
      titleText: '重大事件',
      btnText: '添加事件',
      advanced: false,
      pathUrl: '/home/events/addImportEvent',
      oldUrl: '/home/events/importEvent',
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        },
      },
      rankOptions: [], //事件程度
      //表格数据
      tableList: [],
      requestUrl: 'api-r/rightsaccident/expand/list',
      date: [],
      areaId: '',
      //搜索条件
      filterField: {
        areaId: '', //事件区域
        industryId: '', //行业
        accidentRank: '', //事件程度
        keyWord: '', //企业名称
        typeId: '', //--事件类型（字典）
        accidentId: '', //事件原因（字典）
        accidentDateStart: '',
        accidentDateEnd: '',
      },
      pageSize: 20,
      pageNo: 1,
      type: [], //--事件类型（字典）
      accident: [], //事件原因（字典）

      isShow: false, //列表加载
      loading: true,
      // isQy: false,
      active: -1,
      isShowBtn: false,
      isShowUpdate: '',
      // isShowDetail: ""
      czUser: false,
    }
  },
  computed: { ...mapState(['areasOptions', 'industryOptions']) },
  created() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    if (this.isCzUser()) {
      this.titleText = '欠薪典型案例'
      this.czUser = true
    }
    // this.isQy = localStorage.getItem("loginType") == "QY" ? true : false;
    this.getRank() //事件程度
    this.getAreas({ areaId: this.$store.state.areaInfoArr.areaId })
    this.getSelecData('RIGHTS_TYPE') //事件类型
    this.getSelecData('RIGHTS_ACCIDENT') //事件原因
    //涉及人员INVOLVED_RANGE_NAME
    this.getIndustry({})
    this.isShowBtn =
      this.$store.state.buttonLimitsArr.indexOf('importEvent:add') > -1
    // this.isShowUpdate = this.$store.state.buttonLimitsArr.indexOf('importEvent:update') > -1;
    // this.isShowDetail = this.$store.state.buttonLimitsArr.indexOf('importEvent:detail') > -1;
  },
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    ...mapActions(['getAreas', 'getIndustry']),
    getSelecData(type) {
      var url = 'api-bs/base/dicitem/findbytype'
      this.axios({
        method: 'post',
        url: url,
        data: { type: type },
      })
        .then((res) => {
          if (type == 'RIGHTS_TYPE') {
            this.type = res.data.result
          } else if (type == 'RIGHTS_ACCIDENT') {
            this.accident = res.data.result
          } else if (type == 'INDUSTRY') {
            this.industry = res.data.result
          }
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '获取数据失败',
            type: 'error',
          })
        })
    },

    //行业
    getIndustryId() {
      this.filterField.industryId =
        this.filterField.industryId[this.filterField.industryId.length - 1]
    },

    //获取选中的区域最后一级id
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
    },

    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    getRank() {
      this.axios({
        method: 'post',
        url: 'api-r/rightsdispute/expand/getRank',
        data: {},
      })
        .then((response) => {
          this.rankOptions = response.data.result
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '获取事件程度数据失败',
            type: 'error',
          })
        })
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },

    toDetail(index, row) {
      this.$router.push({
        path: '/home/events/detailImportEvent',
        name: 'detailImportEvent',
        query: {
          id: row.id,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/events/importEvent',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    toEdit(index, row) {
      this.$router.push({
        path: '/home/events/editImportEvent',
        name: 'editImportEvent',
        query: {
          id: row.id,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/events/importEvent',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    dateForma: function (row, column) {
      var date = row[column.applyDate]
      if (date == undefined) {
        return '--'
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getStartTimeValue(val) {
      if (val) {
        this.filterField.accidentDateStart = val
      } else {
        this.filterField.accidentDateStart = ''
      }
    },
    getEndTimeValue(val) {
      if (val) {
        this.filterField.accidentDateEnd = val
      } else {
        this.filterField.accidentDateEnd = ''
      }
    },
    toDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        cancelButtonClass: 'btn-custom-cancel',
        type: 'warning',
      })
        .then(() => {
          this.del(row.id)
        })
        .catch(() => {
          // this.$message.error("删除失败");
        })
    },
    del(id) {
      this.axios({
        method: 'post',
        url: 'api-r/rightsaccident/base/delete',
        data: { id: id },
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.$message({
              message: '删除成功',
              type: 'success',
            })
            this.$nextTick(function () {
              this.$refs.pagination.getTableListData()
            })
          } else {
            this.$message({
              message: response.data.errorMsg,
              type: 'error',
            })
          }
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 1000,
            message: '删除失败',
            type: 'error',
          })
        })
    },
  },
}
</script>

<style scoped lang="less">
.saveBtn {
  margin-right: 24px;
  margin-top: 15.5px;
}

.mainCon {
  height: calc(100% - 67px);
}

.nav ul {
  list-style: none;
}

.nav li {
  display: block;
  float: left;
}
.el-tooltip__popper {
  max-width: 30% !important;
  word-break: break-all;
}
@media only screen and (max-width: 1366px) {
  /deep/.searchInputWidth .el-form--inline .el-form-item__label {
    width: 80px;
  }
}
@import '../../../static/css/filterArea.less';
</style>
